// CSymbianOSUnitAppUi.cpp
//
// Created by Penrillian, Smartphone Software Innovators 
// See http://www.penrillian.com


#include "SymbianOSUnit.h"
#include "Logger.h"
#include "TestDriver.h"
#include <w32std.h> // RWsSession 

_LIT(KTestLogsDir, "SymbianOSUnit");
const TInt KMaxCmds = 2000;

void CSymbianOSUnitAppUi::ConstructL()
{
#ifdef UIQ3
	CQikAppUi::ConstructL();
	iAppView = CSymbianOSUnitAppView::NewL(*this);
	AddViewL(*iAppView);
#else
    BaseConstructL();
	iAppView = CSymbianOSUnitAppView::NewL(ClientRect());
#endif

	Logger::StartL(KTestLogsDir);
	
	// need this to support series60 scrollbar
#if defined SERIES60_3RD
	iAppView->SetMopParent(this);

#endif
	iEikonEnv->WsSession().SetAutoFlush(ETrue);
	iIdleCallback = CIdle::NewL( CActive::EPriorityIdle );
}

#ifdef UIQ3
CDesCArray* CSymbianOSUnitAppUi::SuiteNamesLC()
{
	CDesCArrayFlat* suiteNames = new (ELeave) CDesCArrayFlat(3);
	CleanupStack::PushL(suiteNames);
	TestDriver::TestSuiteNamesL(*suiteNames);
	return suiteNames;
}

#endif


CSymbianOSUnitAppUi::~CSymbianOSUnitAppUi()
{
	Logger::Finish();

#ifndef UIQ3
	delete iAppView;
#endif
	delete iIdleCallback;
}


#ifndef UIQ3

void CSymbianOSUnitAppUi::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane* aMenuPane)
{
	if(aResourceId == R_FILE_FIRST_MENU)
	{
		CDesCArrayFlat* suiteNames = new (ELeave) CDesCArrayFlat(3);
		CleanupStack::PushL(suiteNames);
		TestDriver::TestSuiteNamesL(*suiteNames);
		if(suiteNames->MdcaCount() == 0)
		{
			aMenuPane->SetItemDimmed(ECmdRunTest, ETrue);
		}
		else
		{
			CEikMenuPaneItem::SData data;
			data.iCascadeId = 0; 
			data.iFlags = 0; 
			for(TInt i = 0; i < suiteNames->MdcaCount(); i++)
			{
				data.iCommandId = ECmdRunTest + i +1;
				data.iText = suiteNames->MdcaPoint(i);
				aMenuPane->InsertMenuItemL(data, i+1);
			}
		}
		CleanupStack::PopAndDestroy(suiteNames);
	}

}

void CSymbianOSUnitAppUi::HandleCommandL(TInt aCommand)
{	
	switch (aCommand)
	{					
		case EEikCmdExit: 
		{
			Exit();
			break;
		}
		case ECmdRunTest:
		{
			RunTestsL();
			break;
		}
		default:
		{
			RunTestsL(aCommand - ECmdRunTest -1);
			break;
		}
	}
}

void CSymbianOSUnitAppUi::RunTestsL(TInt aSuite)
{
	TRAPD(err, iAppView->ClearViewL() );
	TRAP(err,iAppView->RunSuitesL(aSuite));		
	if(err != KErrNone)
	{
		User::InfoPrint(_L("Leave: could not run tests"));
	}
}

void CSymbianOSUnitAppUi::HandleStatusPaneSizeChange()
{
	iAppView->SetRect( ClientRect() );
}
#endif //not UIQ3

TBool CSymbianOSUnitAppUi::ProcessCommandParametersL(TApaCommand aCommand,TFileName& aDocumentName,const TDesC8& aTail) 
{
	TPtrC tail( (TText*)aTail.Ptr(), aTail.Length()/2);
	TBool result = CEikAppUi::ProcessCommandParametersL(aCommand, aDocumentName, aTail );
	if (tail.Length())
	{
		iIdleCallback->Start( TCallBack( RunTestsCallbackL, this ) );
	}
	return result;
}

TInt CSymbianOSUnitAppUi::RunTestsCallbackL( void* aPointer )
{
	CSymbianOSUnitAppUi* self = static_cast<CSymbianOSUnitAppUi*>( aPointer );
#if defined SERIES60_3RD
	self->RunTestsL();
#else // defined UIQ3
	self->iAppView->RunSuitesL();
#endif
	self->Exit();
	return ETrue; // Don't call again.
}

